<?php
class CodeGenerator
{
	private $_appName;
	private $_moduleName;
	private $_actionName;

	public function main()
	{
		$this->_appName = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'frontend';
		$this->_moduleName = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 'index';
		$this->_actionName = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][3] : 'Index';
		$this->initProject();
		$this->initApp();
		$this->initModule();
		$this->initAction();
	}

	public function initProject()
	{
		if(!is_dir('app'))
		{
			mkdir('app');
			mkdir('cli_entrance');
			mkdir('conf');
			mkdir('dist');
			mkdir('lib/3rd_party',null,true);
			mkdir('lib/dao',null,true);
			mkdir('lib/service',null,true);
			mkdir('lib/util',null,true);
			mkdir('web_entrance',null,true);

			file_put_contents('conf/conf.php', "<?php
\$myProjConfig = array();
include('db_server.php');
return \$myProjConfig;");

			file_put_contents('conf/conf_dev.php', "<?php
\$myProjConfig = array();
include('db_server.php');
return \$myProjConfig;");

			file_put_contents('conf/db_server.php', "<?php
\$myProjConfig['db_server'] = array(
	'global' => array(
		'the_only_node' => array(
			'master' => array(
				'the_only_host' => array(
					'adapter' => 'pdoMysql',
					'username' => '',
					'password' => '',
					'dbname' => '',
)))));");
		}
	}

	public function initApp()
	{
		$appDir = "app/$this->_appName";
		if (!is_dir($appDir))
		{
			$appAbstractAction = ucfirst($this->_appName) . "Action";
			$appAbstractView = ucfirst($this->_appName) . "View";
			mkdir("$appDir/layout",null,true);
			mkdir("$appDir/lib/component",null,true);
			mkdir("web_entrance/$this->_appName",null,true);

			$kiwiRuntimeHome = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR;

			file_put_contents("web_entrance/$this->_appName/$this->_appName.php", "<?php
include('{$kiwiRuntimeHome}kiwi.php');
Kiwi::setEnvMode('production');
Kiwi::run(array(),'index','Index');");

			file_put_contents("web_entrance/$this->_appName/{$this->_appName}_dev.php", "<?php
include('{$kiwiRuntimeHome}kiwi.php');
Kiwi::run(array(
	'app_name' => '$this->_appName',
	'config_file' => '../../conf/conf_dev.php',
),'index','Index');");

			file_put_contents("app/$this->_appName/lib/$appAbstractAction.php", "<?php
class $appAbstractAction extends Action
{
	public function __construct()
	{
		parent::__construct();
		\$this->view = new $appAbstractView();
	}
}");

			file_put_contents("app/$this->_appName/lib/$appAbstractView.php", "<?php
class $appAbstractView extends View
{
	public function __construct()
	{
		\$this->setResponseType('html');
		\$this->setLayout('frontpage');
	}
}");
			file_put_contents("app/$this->_appName/layout/frontpage.php", "<html>
<head>
	<title><?php echo \$this->getTitle() ?></title>
	<base href=\"<?php echo \$this->uri['base']; ?>\" />
</head>
<body>
page head
<hr />
<?php
include(\$this->getTemplateDir() . \$this->getTemplate() . '.php');
?>
<hr />
page tail
</body>
</html>");

			file_put_contents("app/$this->_appName/layout/blank.php", "<?php
include(\$this->getTemplateDir() . \$this->getTemplate() . '.php');");
		}
	}

	public function initModule()
	{
		$moduleDir = "app/$this->_appName/module/$this->_moduleName";
		if (!is_dir($moduleDir))
		{
			mkdir("$moduleDir/action", null, true);
			mkdir("$moduleDir/view", null, true);
		}
	}

	public function initAction()
	{
		$actionClass = $this->_actionName . 'Action';
		$actionFile = "app/$this->_appName/module/$this->_moduleName/action/$actionClass.php";
		if (!file_exists($actionFile))
		{
			$parentAction = ucfirst($this->_appName) . 'Action';
			
			file_put_contents($actionFile, "<?php
class $actionClass extends $parentAction
{
	public function execute()
	{
		\$this->view->render();
	}
}");
			file_put_contents("app/$this->_appName/module/$this->_moduleName/view/$this->_actionName.php", "<pre>
app: $this->_appName
module: $this->_moduleName
action: $this->_actionName
</pre>");
		}
	}
}
$codeGenerator = new CodeGenerator();
$codeGenerator->main();